package com.bawei.mall.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.exception.ServiceException;
import com.bawei.mall.user.domain.MallUserFootprint;
import com.bawei.mall.user.domain.UserList;
import com.bawei.mall.user.domain.request.UserInformati;
import com.bawei.mall.user.mapper.FootprintMapper;
import com.bawei.mall.user.service.FootprintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：张余庆
 * @Package：com.bawei.mall.user.service.Impl
 * @Project：mall_cloud
 * @name：FootprintServiceImpl
 * @Date：2024/7/1 20:31
 */
@Service
public class FootprintServiceImpl implements FootprintService {

    @Autowired
    private FootprintMapper mapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 功能：查询用户足迹表
     * @return
     */
    @Override
    public UserList list(Integer pageNum, Integer pageSize) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        UserList userList = new UserList();
        List<MallUserFootprint> list = mapper.list(userId,pageSize,pageNum);
        userList.setList(list);
        return userList;
    }

    /**
     * 功能：删除用户足迹
     * @param id
     */
    @Override
    public void delFootprint(Integer id) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.delFootprint(id,userId);
        if(i<=0){
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 功能：添加用户足迹
     * @param footprint
     */
    @Override
    public void add(MallUserFootprint footprint) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        footprint.setUserId(userId);
        Integer i = mapper.add(footprint);
        if(i<=0){
            throw new RuntimeException("添加失败");
        }
    }
}
